package com.ruoyi.system.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.system.frontierCommand.domain.TPatrolRecord;
import com.ruoyi.system.mobile.domain.PatrolRecord;

import com.ruoyi.system.mobile.bo.TPatrolRecordAddBo;
import com.ruoyi.system.mobile.bo.TPatrolRecordEditBo;
import com.ruoyi.system.mobile.bo.TPatrolRecordQueryBo;
import com.ruoyi.system.mobile.mapper.PatrolRecordMapper;
import com.ruoyi.system.mobile.service.PatrolRecordService;
import com.ruoyi.system.mobile.vo.PatrolRecordVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡护记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-05-20
 */
@Service
public class PatrolRecordServiceImpl extends ServiceImpl<PatrolRecordMapper, PatrolRecord> implements PatrolRecordService {

    private PatrolRecordMapper tPatrolRecordMapper;

    @Override
    public PatrolRecordVo queryById(Long id){
        PatrolRecord db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, PatrolRecordVo.class);
    }

    @Override
    public List<PatrolRecordVo> queryList(TPatrolRecordQueryBo bo) {
//        LambdaQueryWrapper<TPatrolRecord> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getBeginTime() != null, TPatrolRecord::getBeginTime, bo.getBeginTime());
//        lqw.eq(bo.getEndTime() != null, TPatrolRecord::getEndTime, bo.getEndTime());

        return this.baseMapper.selectDTO(bo);
        //return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<PatrolRecordVo> entity2Vo(Collection<TPatrolRecord> collection) {
        List<PatrolRecordVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, PatrolRecordVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TPatrolRecord> page = (Page<TPatrolRecord>)collection;
            Page<PatrolRecordVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TPatrolRecordAddBo bo) {
        PatrolRecord add = BeanUtil.toBean(bo, PatrolRecord.class);
        validEntityBeforeSave(add,1);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TPatrolRecordEditBo bo) {
        PatrolRecord update = BeanUtil.toBean(bo, PatrolRecord.class);
        validEntityBeforeSave(update,2);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     * @param type 操作类型
     */
    private void validEntityBeforeSave(PatrolRecord entity, int type){
        //TODO 做一些数据校验,如唯一约束
        QueryWrapper<PatrolRecord> queryWrapper=new QueryWrapper();
        queryWrapper.lambda().eq(PatrolRecord::getAppUserId,entity.getAppUserId());
        queryWrapper.lambda().orderByDesc(PatrolRecord::getCreateTime);
        queryWrapper.last("limit 0,1");
        PatrolRecord patrolRecord = this.baseMapper.selectOne(queryWrapper);
        if(type==1){
            if(patrolRecord!=null&&patrolRecord.getEndTime()==null){
                throw new BaseException("您还有未结束的巡护记录,请先结束再开始！");
            }
        }else{
            if(patrolRecord!=null){
                entity.setId(patrolRecord.getId());
            }
        }

//        this.queryById(entity.getId());
//        if(){
//
//        }
    }


}
